package com.qyhl.webtv.module_broke.scoop;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gyf.immersionbar.ImmersionBar;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.MPermissionUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.broke.ScoopTopicBean;
import com.qyhl.webtv.commonlib.entity.circle.ScoopListBean;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_broke.R;
import com.qyhl.webtv.module_broke.R2;
import com.qyhl.webtv.module_broke.scoop.itemview.ScoopNoPhotoViewDelegete;
import com.qyhl.webtv.module_broke.scoop.itemview.ScoopPhotoViewDelegete;
import com.qyhl.webtv.module_broke.scoop.itemview.ScoopRadioViewDelegete;
import com.qyhl.webtv.module_broke.scoop.itemview.ScoopVideoViewDelegete;
import com.qyhl.webtv.module_broke.utils.radiobutton.MediaManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.umeng.analytics.MobclickAgent;
import com.wangjie.androidbucket.utils.ABTextUtil;
import com.wangjie.rapidfloatingactionbutton.RapidFloatingActionButton;
import com.wangjie.rapidfloatingactionbutton.RapidFloatingActionHelper;
import com.wangjie.rapidfloatingactionbutton.RapidFloatingActionLayout;
import com.wangjie.rapidfloatingactionbutton.contentimpl.labellist.RFACLabelItem;
import com.wangjie.rapidfloatingactionbutton.contentimpl.labellist.RapidFloatingActionContentLabelList;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * Created by qyhl on 2017/11/7.
 * 内容首页
 */

public class ScoopHomeFragment extends BaseFragment implements ScoopHomeContract.ScoopHomeView, RapidFloatingActionContentLabelList.OnRapidFloatingActionContentLabelListListener {
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.listview)
    RecyclerView listview;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.label_list_sample_rfab)
    RapidFloatingActionButton rfaButton;
    @BindView(R2.id.label_list_sample_rfal)
    RapidFloatingActionLayout rfaLayout;
    @BindView(R2.id.back_btn)
    ImageView backBtn;
    @BindView(R2.id.title_layout)
    RelativeLayout titleLayout;

    private ScoopHomePresenter mPresenter;
    private String title;

    TagFlowLayout mFlowLayout;
    private String brokeId = "0";//分页Id

    private int topicId;//话题id

    private int skipTopicId = 0;

    private RapidFloatingActionHelper rfabHelper;
    private EmptyWrapper emptyWrapper;

    public void setTitle(String title) {
        this.title = title;
    }

    private List<ScoopTopicBean> mTagList;

    private List<ScoopListBean> mDataList;

    private int index = 0;//记录当前标签

    private boolean isLoadMore;

    private boolean isTop = false;

    private boolean isSingle = false;

    public void setTop(boolean top) {
        isTop = top;
    }

    private ScoopNoPhotoViewDelegete scoopNoViewDelegete;
    private ScoopPhotoViewDelegete scoopPhotoViewDelegete;
    private ScoopRadioViewDelegete scoopRadioViewDelegete;
    private ScoopVideoViewDelegete scoopVideoViewDelegete;

    public void setSkipTopicId(int skipTopicId) {
        this.skipTopicId = skipTopicId;
    }

    public void setTopicId(int topicId) {
        this.topicId = topicId;
    }

    public void setSingle(boolean single) {
        isSingle = single;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.broke_fragment_scoop_home, null);
    }

    @Override
    protected void create() {
        mPresenter = new ScoopHomePresenter(this);
        initView();
        loadData();

    }

    @Override
    protected void setListener() {
        setListeners();
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .statusBarColor(R.color.white)
                .fitsSystemWindows(true)
                .init();
    }

    public static ScoopHomeFragment newInstance(String title) {
        ScoopHomeFragment fragment = new ScoopHomeFragment();
        fragment.setTitle(title);
        return fragment;
    }

    public static ScoopHomeFragment newInstance(boolean isSingle, int topicId) {
        ScoopHomeFragment fragment = new ScoopHomeFragment();
        fragment.setSingle(isSingle);
        fragment.setSkipTopicId(topicId);
        fragment.setTopicId(topicId);
        return fragment;
    }

    public static ScoopHomeFragment newInstance(String title, boolean isTop) {
        ScoopHomeFragment fragment = new ScoopHomeFragment();
        fragment.setTitle(title);
        fragment.setTop(isTop);
        return fragment;
    }


    /**
     * 加载数据
     */
    private void loadData() {
        //下拉刷新
        if (isSingle) {
            brokeId = "0";
            String userName = CommonUtils.getInstance().getUserName();
            mPresenter.getDatas(userName, topicId + "", brokeId);
        } else {
            mPresenter.getTopic(true);
        }

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //行为数据统计
            ActionLogUtils.getInstance().onResume(this.getActivity(), ActionConstant.BROKENEWS);
        } else {
            //行为数据统计
            ActionLogUtils.getInstance().onPause(this.getActivity(), ActionConstant.BROKENEWS);
            GSYVideoManager.releaseAllVideos();
            MediaManager me = MediaManager.getIntance();
            if (me.isPlay()) {
                //停止播放语音
                me.release();
                //停止动画
                me.getTextView().endAnimation();

                //恢复时间
                me.getTextView().setContent(me.getContentBean().getBrokeNews().getAudioDuration());
            }
        }
    }

    //初始化
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        if (isTop) {
            backBtn.setVisibility(View.VISIBLE);
        } else {
            backBtn.setVisibility(View.GONE);
        }

        BusFactory.getBus().register(this);

        mTagList = new ArrayList<>();
        mDataList = new ArrayList<>();


        //配置刷新
        refresh.setEnableLoadMore(true);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));

        //浮动菜单初始化
        RapidFloatingActionContentLabelList rfaContent = new RapidFloatingActionContentLabelList(getContext());
        rfaContent.setOnRapidFloatingActionContentLabelListListener(this);
        List<RFACLabelItem> items = new ArrayList<>();
        RFACLabelItem<Integer> item1 = new RFACLabelItem<>();
        if (CommonUtils.getInstance().getSite() == 174) {
            item1.setLabel("图文");
        } else {
            item1.setLabel("图文内容");
        }
        item1.setResId(R.drawable.scoop_photo)
                .setIconNormalColor(getContext().getResources().getColor(R.color.white))
                .setIconPressedColor(getContext().getResources().getColor(R.color.scoop_add_press))
                .setLabelColor(getContext().getResources().getColor(R.color.global_gray_lv2))
                .setLabelSizeSp(15)
                .setWrapper(0);

        items.add(item1);

        RFACLabelItem<Integer> item2 = new RFACLabelItem<>();
        if (CommonUtils.getInstance().getSite() == 174) {
            item2.setLabel("视频");
        } else {
            item2.setLabel("视频内容");
        }
        item2.setIconNormalColor(getContext().getResources().getColor(R.color.white))
                .setIconPressedColor(getContext().getResources().getColor(R.color.scoop_add_press))
                .setResId(R.drawable.scoop_video)
                .setLabelColor(getContext().getResources().getColor(R.color.global_gray_lv2))
                .setLabelSizeSp(15)
                .setWrapper(1);
        items.add(item2);

        rfaContent.setItems(items)
                .setIconShadowRadius(ABTextUtil.dip2px(getContext(), 5))
                .setIconShadowColor(getContext().getResources().getColor(R.color.global_gray_lv4))
                .setIconShadowDy(ABTextUtil.dip2px(getContext(), 5))
                .setIconShadowDx(ABTextUtil.dip2px(getContext(), 5));

        rfabHelper = new RapidFloatingActionHelper(
                getContext(),
                rfaLayout,
                rfaButton,
                rfaContent
        ).build();

        View headView = LayoutInflater.from(getContext()).inflate(R.layout.broke_header_scoop_item, null);
        mFlowLayout = (TagFlowLayout) headView.findViewById(R.id.scoop_tag_layout);
        if (isSingle) {
            scoopNoViewDelegete = new ScoopNoPhotoViewDelegete(false, getActivity());
            scoopPhotoViewDelegete = new ScoopPhotoViewDelegete(false, getActivity());
            scoopRadioViewDelegete = new ScoopRadioViewDelegete(false, getActivity());
            scoopVideoViewDelegete = new ScoopVideoViewDelegete(false, getActivity());
        } else {
            scoopNoViewDelegete = new ScoopNoPhotoViewDelegete(true, getActivity());
            scoopPhotoViewDelegete = new ScoopPhotoViewDelegete(true, getActivity());
            scoopRadioViewDelegete = new ScoopRadioViewDelegete(true, getActivity());
            scoopVideoViewDelegete = new ScoopVideoViewDelegete(true, getActivity());
        }
        MultiItemTypeAdapter<ScoopListBean> mAdapter = new MultiItemTypeAdapter<ScoopListBean>(getContext(), mDataList);

        mAdapter.addItemViewDelegate(scoopNoViewDelegete);
        mAdapter.addItemViewDelegate(scoopPhotoViewDelegete);
        mAdapter.addItemViewDelegate(scoopRadioViewDelegete);
        mAdapter.addItemViewDelegate(scoopVideoViewDelegete);


        if (isSingle) {
            titleLayout.setVisibility(View.GONE);
            headView.setVisibility(View.GONE);
        } else {
            titleLayout.setVisibility(View.VISIBLE);
            headView.setVisibility(View.VISIBLE);
            mTitle.setText(title);
        }


        HeaderAndFooterWrapper mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter);
        mHeaderAndFooterWrapper.addHeaderView(headView);
        emptyWrapper = new EmptyWrapper(mHeaderAndFooterWrapper);
        emptyWrapper.setEmptyView(R.layout.layout_common_emptyview);

        listview.addItemDecoration(new RecycleViewDivider(
                getContext(), LinearLayoutManager.HORIZONTAL, 5, ContextCompat.getColor(getContext(), R.color.global_background)));
        listview.setLayoutManager(new LinearLayoutManager(getContext()));
        listview.setAdapter(emptyWrapper);

        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (position != 0) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", mDataList.get(position - 1).getBrokeNews().getId() + "");
                    RouterManager.router(ARouterPathConstant.SCOOP_DETAIL_ACTIVIT, bundle);

                    ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.SPECIADETAIL);
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

    }

    //设置监听
    private void setListeners() {
        //刷新
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                brokeId = "0";
                isLoadMore = false;

                if (isSingle) {
                    String userName = CommonUtils.getInstance().getUserName();
                    mPresenter.getDatas(userName, topicId + "", brokeId);
                } else {
                    mPresenter.getTopic(true);
                }
                MediaManager me = MediaManager.getIntance();
                if (me.isPlay()) {
                    //停止播放语音
                    me.release();
                    //停止动画
                    me.getTextView().endAnimation();

                    //恢复时间
                    me.getTextView().setContent(me.getContentBean().getBrokeNews().getAudioDuration());
                }
            }
        });
        //加载
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                isLoadMore = true;
                String userName = CommonUtils.getInstance().getUserName();
                mPresenter.getDatas(userName, topicId + "", brokeId);
            }
        });
        //重载
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                brokeId = "0";
                loadMask.setReloadButtonText("加载中...");
                if (isSingle) {
                    String userName = CommonUtils.getInstance().getUserName();
                    mPresenter.getDatas(userName, topicId + "", brokeId);
                } else {
                    mPresenter.getTopic(true);
                }
            }
        });

        backBtn.setOnClickListener(view -> BusFactory.getBus().post(new Event.finishActivity()));


    }

    /**
     * 设置话题
     *
     * @param beanList
     */
    @Override
    public void setTopic(List<ScoopTopicBean> beanList) {
        rfaButton.setVisibility(View.VISIBLE);
        mTagList = beanList;
        mTagList.add(0, new ScoopTopicBean("全部", -1, CommonUtils.getInstance().getSite()));
        mFlowLayout.setMaxSelectCount(1);
        mFlowLayout.setAdapter(new TagAdapter<ScoopTopicBean>(mTagList) {
            @SuppressLint("SetTextI18n")
            @Override
            public View getView(FlowLayout parent, int position, ScoopTopicBean tag) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.broke_item_scoop_add_tag,
                        mFlowLayout, false);
                if (position == 0) {
                    tv.setText(tag.getName());
                } else {
                    tv.setText("#" + tag.getName() + "#");
                }
                return tv;
            }
        });

        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {

            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                MediaManager me = MediaManager.getIntance();
                if (me.isPlay()) {
                    //停止播放语音
                    me.release();
                    //停止动画
                    me.getTextView().endAnimation();

                    //恢复时间
                    me.getTextView().setContent(me.getContentBean().getBrokeNews().getAudioDuration());
                }
                GSYVideoManager.releaseAllVideos();
                index = position;
                topicId = mTagList.get(position).getId();
                if (topicId == -1) {
                    scoopVideoViewDelegete.setMain(true);
                    scoopRadioViewDelegete.setMain(true);
                    scoopNoViewDelegete.setMain(true);
                    scoopPhotoViewDelegete.setMain(true);
                } else {
                    scoopVideoViewDelegete.setMain(false);
                    scoopRadioViewDelegete.setMain(false);
                    scoopNoViewDelegete.setMain(false);
                    scoopPhotoViewDelegete.setMain(false);
                }
                String userName = CommonUtils.getInstance().getUserName();
                mPresenter.getDatas(userName, topicId + "", "0");
                return false;
            }
        });

        if (index == 0) {
            topicId = -1;
        } else {
            try {
                topicId = mTagList.get(index).getId();
            } catch (Exception e) {
                index = 0;
                topicId = -1;
            }
        }

        mFlowLayout.getAdapter().setSelectedList(index);//预选第一个
        String userName = CommonUtils.getInstance().getUserName();
        mPresenter.getDatas(userName, topicId + "", brokeId);

    }

    //设置内容数据
    @Override
    public void setDatas(List<ScoopListBean> beanList, boolean isMore) {
        refresh.finishRefresh();
        refresh.finishLoadMore();
        loadMask.setReloadButtonText("点击重试~~");
        loadMask.setStatus(LoadingLayout.Success);
        if (isMore) {
            mDataList.addAll(beanList);
        } else {
            mDataList.clear();
            mDataList.addAll(beanList);
        }
        brokeId = beanList.get(beanList.size() - 1).getBrokeNews().getId() + "";
        emptyWrapper.notifyDataSetChanged();
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        rfaButton.setVisibility(View.GONE);
        loadMask.setErrorText(msg);
        loadMask.setErrorImage(R.drawable.error_content);
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 无内容
     *
     * @param msg
     */
    @Override
    public void setEmpty(String msg) {
        refresh.finishRefresh();
        rfaButton.setVisibility(View.GONE);
        loadMask.setErrorText(msg);
        loadMask.setErrorImage(R.drawable.empty_content);
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 无网络
     *
     * @param msg
     */
    @Override
    public void setNetError(String msg) {
        refresh.finishRefresh();
        rfaButton.setVisibility(View.GONE);
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setDataError(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setErrorText(msg);
        loadMask.setStatus(LoadingLayout.Error);
    }

    //无内容
    @Override
    public void setDataEmpty(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        if (isLoadMore) {
            loadMask.setStatus(LoadingLayout.Success);
        } else {
            loadMask.setEmptyText(msg);
            loadMask.setStatus(LoadingLayout.Empty);
        }
    }

    //无网络
    @Override
    public void setDataNetError(String msg) {
        refresh.finishLoadMore();
        refresh.finishRefresh();
        loadMask.setNoNetworkText(msg);
        loadMask.setStatus(LoadingLayout.No_Network);
    }

    //toast
    @Override
    public void showMessage(String msg) {
        refresh.finishRefresh();
        Toasty.normal(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    //data toast
    @Override
    public void showDataMessage(String msg) {
        loadMask.setStatus(LoadingLayout.Success);
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (!getUserVisibleHint() && getContext() != null) {
            Toasty.normal(getContext(), msg, Toast.LENGTH_SHORT).show();
        }
    }

    //日志
    @Override
    public void showLog(String msg, boolean isMore) {
        loadMask.setStatus(LoadingLayout.Success);
        refresh.finishRefresh();
        refresh.finishLoadMore();
        if (isMore) {
            Log.i("load_more", msg);
        } else {
            mDataList.clear();
            emptyWrapper.notifyDataSetChanged();
        }

    }


    @Override
    public void onRFACItemLabelClick(int position, RFACLabelItem item) {
        menuClick(position);
        rfabHelper.toggleContent();
    }

    @Override
    public void onRFACItemIconClick(int position, RFACLabelItem item) {
        menuClick(position);
        rfabHelper.toggleContent();
    }

    /**
     * 浮动按钮点击
     *
     * @param position
     */
    private void menuClick(int position) {
        final Bundle bundle = new Bundle();
        bundle.putSerializable("taglist", (Serializable) mTagList);
        switch (position) {
            case 0:
                MPermissionUtils.requestPermissionsResult(ScoopHomeFragment.this, 10, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new MPermissionUtils.OnPermissionListener() {
                    @Override
                    public void onPermissionGranted() {
                        CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                            @Override
                            public void login(boolean isLogin) {
                                if (isLogin) {

                                    bundle.putInt("topId", skipTopicId);
                                    RouterManager.router(ARouterPathConstant.SCOOP_ADD_PIC, bundle, R.anim.open_up, R.anim.close_down);
                                    ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.ADDSCOOP);
                                } else {
                                    RouterManager.routerLogin(getContext(), 0);
                                }
                            }

                            @Override
                            public void error(String s) {

                            }
                        });

                    }

                    @Override
                    public void onPermissionDenied() {
                        MPermissionUtils.showTipsDialog(getActivity());
                    }
                });

                break;
            case 1:
                MPermissionUtils.requestPermissionsResult(ScoopHomeFragment.this, 11, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new MPermissionUtils.OnPermissionListener() {
                    @Override
                    public void onPermissionGranted() {

                        CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                            @Override
                            public void login(boolean isLogin) {
                                if (isLogin) {

                                    bundle.putInt("topId", skipTopicId);
                                    RouterManager.router(ARouterPathConstant.SCOOP_ADD_VIDEO, bundle, R.anim.open_up, R.anim.close_down);

                                    ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.ADDSCOOP);
                                } else {
                                    RouterManager.routerLogin(getContext(), 0);
                                }
                            }

                            @Override
                            public void error(String s) {

                            }
                        });


                    }

                    @Override
                    public void onPermissionDenied() {
                        MPermissionUtils.showTipsDialog(getActivity());
                    }
                });

                break;
            case 2:
                MPermissionUtils.requestPermissionsResult(ScoopHomeFragment.this, 11, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO
                }, new MPermissionUtils.OnPermissionListener() {
                    @Override
                    public void onPermissionGranted() {
                        CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                            @Override
                            public void login(boolean isLogin) {
                                if (isLogin) {

                                    bundle.putInt("topId", skipTopicId);
                                    RouterManager.router(ARouterPathConstant.SCOOP_ADD_RADIO, bundle, R.anim.open_up, R.anim.close_down);

                                    ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.ADDSCOOP);
                                } else {
                                    RouterManager.routerLogin(getContext(), 0);
                                }
                            }

                            @Override
                            public void error(String s) {

                            }
                        });

                    }

                    @Override
                    public void onPermissionDenied() {
                        MPermissionUtils.showTipsDialog(getActivity());
                    }
                });
                break;
            default:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshMessage(Event.refreshMessage event) {
        if (event != null) {
            ScoopListBean bean = event.getBean();
            for (int i = 0; i < mDataList.size(); i++) {
                if (mDataList.get(i).getBrokeNews().getId() == bean.getBrokeNews().getId()) {
                    mDataList.get(i).setIsAttention(bean.getIsAttention());
                    mDataList.get(i).setAttention(bean.getAttention());
                }
            }
            emptyWrapper.notifyDataSetChanged();
        }
    }

    /**
     * eventBus 接收器
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(Event.LoginMessage event) {
        String userName = CommonUtils.getInstance().getUserName();
        if (event != null) {
            brokeId = "0";
            if (event.isLogin()) {//登录
                mPresenter.getDatas(userName, topicId + "", brokeId);
            } else {//注销
                mPresenter.getDatas(userName, topicId + "", brokeId);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        MediaManager me = MediaManager.getIntance();
        if (me.isPlay()) {
            //停止播放语音
            me.release();
            //停止动画
            me.getTextView().endAnimation();

            //恢复时间
            me.getTextView().setContent(me.getContentBean().getBrokeNews().getAudioDuration());
        }
        GSYVideoManager.releaseAllVideos();
        super.onPause();
        //umeng
        MobclickAgent.onPageEnd("内容首页");
        MobclickAgent.onPause(getContext());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            GSYVideoManager.releaseAllVideos();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BusFactory.getBus().unregister(this);
    }


}
